package com.gzhryc.shared_device.oem.micro.queue.handlers.order;

import com.gzhryc.common.event.AbsDelayHandler;
import com.gzhryc.common.event.DelayHandleService;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNOrder;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNOrderService;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNShopOrder;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNShopOrderService;
import com.gzhryc.shared_device.cdb.code.dao.db.CDBOrder;
import com.gzhryc.shared_device.cdb.code.services.CDBOrderService;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZOrder;
import com.gzhryc.shared_device.cdcz.code.services.CDCZOrderService;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.order.event.OrderFinishEvent;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.oem.code.profit.event.UserProfitDivideEvent;
import com.gzhryc.shared_device.oem.code.statistics.services.*;
import com.gzhryc.shared_device.pcl.code.dao.db.PCLOrder;
import com.gzhryc.shared_device.pcl.code.services.PCLOrderService;

import java.util.Date;

public class OrderFinishEventHandler extends AbsDelayHandler<OrderFinishEvent> {

    static Logger log = Logger.getLogger(OrderFinishEventHandler.class);

    @Override
    public boolean execute(OrderFinishEvent event) {
        log.info("订单完成事件：{{0:json}}", event);
        try {
            PlacePhysicalStore place = null;
            Date finishDate = null;
            Long money = 0L;
            Long payMoney = 0L;
            if (EBusinessType.CDCZ.index().equals(event.getBusinessType())) {
                CDCZOrder cdczOrder = CDCZOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                place = PlacePhysicalStoreService.self().getById(cdczOrder.getPlaceId());
                money = cdczOrder.getMoney();
                payMoney = cdczOrder.getIncomeMoney();
                finishDate = cdczOrder.getFinishDate();
            }else if (EBusinessType.CDB.index().equals(event.getBusinessType())) {
                CDBOrder cdbOrder = CDBOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                place = PlacePhysicalStoreService.self().getById(cdbOrder.getPlaceId());
                money = cdbOrder.getMoney();
                payMoney = cdbOrder.getIncomeMoney();
                finishDate = cdbOrder.getFinishDate();
            }else if (EBusinessType.PCL.index().equals(event.getBusinessType())) {
                PCLOrder pclOrder = PCLOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                place = PlacePhysicalStoreService.self().getById(pclOrder.getPlaceId());
                money = pclOrder.getMoney();
                payMoney = pclOrder.getIncomeMoney();
                finishDate = pclOrder.getFinishDate();
            }else if (EBusinessType.BXSCN.index().equals(event.getBusinessType())) {
                BXSCNOrder bxscnOrder = BXSCNOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                place = PlacePhysicalStoreService.self().getById(bxscnOrder.getPlaceId());
                money = bxscnOrder.getMoney();
                payMoney = bxscnOrder.getIncomeMoney();
                finishDate = bxscnOrder.getFinishDate();
            }else if (EBusinessType.BXSCN_SHOP.index().equals(event.getBusinessType())) {
                BXSCNShopOrder bxscnShopOrder = BXSCNShopOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
                place = PlacePhysicalStoreService.self().getById(bxscnShopOrder.getPlaceId());
                money = bxscnShopOrder.getMoney();
                payMoney = bxscnShopOrder.getIncomeMoney();
                finishDate = bxscnShopOrder.getFinishDate();
            }

            //触发分成
            log.info("{{0}}订单完成，触发分成",event.getTradeNo());
            UserProfitDivideEvent profitDivideEvent = new UserProfitDivideEvent(event.getTradeNo(),event.getCreateDate(),event.getOperateInfo());
            DelayHandleService.get().addEvent(profitDivideEvent);

            //检查订单完成获取优惠券
            //MemberBusinessOrder memberBusinessOrder = MemberBusinessOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
            

            //项目统计
            ProjectDayStatisticsService.self().addOneOrderData(finishDate,money, payMoney);
            //网点统计
            PlaceDayStatisticsService.self().addOneOrderData(place.getId(), place.getName(), place.getAreaName(),finishDate, money, payMoney);
            //区域统计
            CityAreaDayStatisticsService.self().addOneOrderData(place.getAreaCode(),finishDate,money, payMoney);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return true;
    }
}
